﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using FF.Entity;
using FF.Method;

namespace FF
{
    public partial class FormCancleSale : Form
    {
        public const int ACTION_TYPE_ADD = 1;
        public const int ACTION_TYPE_MODIFY_DELETE = 2;
        const string ID_HEADER = "HD-";
        private int actionType;
        //private User user;
        private CancelOrder order;
         public FormCancleSale(string myorderid)
        {
            InitializeComponent();
            //this.user = user;
            this.actionType = ACTION_TYPE_MODIFY_DELETE;
            this.order = CancelOrderMethod.getEntityById(myorderid);
        }
         public FormCancleSale(User user)
        {
            InitializeComponent();
            this.actionType = ACTION_TYPE_ADD;
            //this.user = user;
        }

        private void calculateTotalPrice()
        {
            this.textBoxtotal_prices.Text = ((decimal.Parse(this.textBoxPrice.Text == "" ? "0" : this.textBoxPrice.Text)) * (decimal.Parse(this.textBoxGoodNum.Text == "" ? "0" : this.textBoxGoodNum.Text)) * (decimal.Parse(this.textBoxDiscount.Text == "" ? "0" : this.textBoxDiscount.Text))).ToString();

        }

        private void button_select_customer_Click(object sender, EventArgs e)
        {
            FormCustomerSelect frm = new FormCustomerSelect();
            frm.Owner = this;
            frm.ShowDialog();
        }

        private void button_select_good_Click(object sender, EventArgs e)
        {
            FormGoodSelect frm = new FormGoodSelect(FormGoodSelect.FORM_TYPE_FormCancleSale);
            frm.Owner = this;
            frm.ShowDialog();
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void button_select_price_Click(object sender, EventArgs e)
        {

        }

        private void FormSale_Load(object sender, EventArgs e)
        {
            

        }

        private void textBoxPrice_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar != 8 && !char.IsDigit(e.KeyChar) && e.KeyChar != '.')
            {
                MessageBox.Show("请输入数字");
                e.Handled = true;
            }
        }

        private void textBoxGoodNum_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar != 8 && !char.IsDigit(e.KeyChar) && e.KeyChar != '.')
            {
                MessageBox.Show("请输入数字");
                e.Handled = true;
            }
        }

        private void textBoxDiscount_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar != 8 && !char.IsDigit(e.KeyChar) && e.KeyChar != '.')
            {
                MessageBox.Show("请输入数字");
                e.Handled = true;
            }
        }

        private void textBoxtotal_prices_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar != 8 && !char.IsDigit(e.KeyChar) && e.KeyChar != '.')
            {
                MessageBox.Show("请输入数字");
                e.Handled = true;
            }
        }

       
        private void textBoxPrice_TextChanged(object sender, EventArgs e)
        {
            calculateTotalPrice();
        }


        private void buttonDelete_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("确定删除吗？", "确认", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                if (CancelOrderMethod.delete(this.textBoxCancelSaleId.Text.Trim()) == 1)
                {
                    MessageBox.Show("删除成功");
                    this.Close();
                }
                else
                {
                    MessageBox.Show("删除失败");

                }
            }
        }

        private void buttonOK_Click(object sender, EventArgs e)
        {
            if (this.textBoxGoodNum.Text == "")
            {
                MessageBox.Show("数量不能为空！");
                this.textBoxGoodNum.Focus();
                return;
            }

            this.order = new CancelOrder();
            order.orderId = this.textBoxOrderId.Text;
            //order.userId = user.UserId.ToString();
            //order.userName = user.UserName;
            order.goodId = this.textBoxGoodID.Text;
            order.goodName = this.textBoxGoodName.Text;
            order.customerId = this.textBoxCustomerId.Text;
            order.customerName = this.textBoxCustomerName.Text;
            order.goodPrice = this.textBoxPrice.Text.Trim();
            order.goodNum = this.textBoxGoodNum.Text.Trim();
            order.totalPrices = this.textBoxtotal_prices.Text.Trim();
            //order.moneyReceive = this.textBox_money_receive.Text.Trim();
            order.discount = this.textBoxDiscount.Text.Trim();
            order.receiverName = this.textBoxReceiver_name.Text.Trim();
            order.addDate = this.dateTimePickerAdddate.Value.ToString("s");
            //order.receiveCompany = this.textBoxReceiveCompany.Text.Trim();
            order.remark = this.richTextBoxRemark.Text;

            //order.addYear = this.dateTimePickerAdddate.Value.Year.ToString();
            //order.addMonth = dateTimePickerAdddate.Value.Month.ToString();
            //order.addDay = dateTimePickerAdddate.Value.Day.ToString();
            switch (actionType)
            {
                case ACTION_TYPE_ADD:

                    if (CancelOrderMethod.add(order) == 1)
                    {
                        MessageBox.Show("添加成功");
                        //this.Close();

                        this.textBoxGoodID.Text = "";
                        this.textBoxGoodName.Text = "";
                        this.textBoxCustomerId.Text = "";
                        this.textBoxCustomerName.Text = "";
                        this.textBoxPrice.Text = "";
                        this.textBoxGoodNum.Text = "";
                        this.textBoxtotal_prices.Text = "";
                        //this.textBox_money_receive.Text = "";
                        this.textBoxDiscount.Text = "";
                        this.textBoxReceiver_name.Text = "";

                        //this.textBoxReceiveCompany.Text = "";
                        this.richTextBoxRemark.Text = "";
                    }
                    else
                    {
                        MessageBox.Show("添加失败");


                    }
                    break;
                case ACTION_TYPE_MODIFY_DELETE:
                    if (CancelOrderMethod.update(order) == 1)
                    {
                        MessageBox.Show("修改成功");
                        this.Close();
                    }
                    else
                    {
                        MessageBox.Show("修改失败");

                    }
                    break;

            }
        }

        private void FormCancleSale_Load(object sender, EventArgs e)
        {
            switch (actionType)
            {
                case ACTION_TYPE_ADD:
                    //this.textBoxOrderId.Text = GoodMethod.generateID(ID_HEADER);
                    this.buttonOK.Text = "确定";
                    this.buttonDelete.Visible = false;
                    break;
                case ACTION_TYPE_MODIFY_DELETE:
                    this.Text = "修改/删除退货记录";
                    this.buttonOK.Text = "修改";
                    this.buttonDelete.Visible = true;
                    this.textBoxCancelSaleId.Text = order.cancelOrderId;
                    this.textBoxOrderId.Text = order.orderId;
                    //user.UserId = Convert.ToInt32(order.userId);
                    //user.UserName = order.userName;
                    this.textBoxGoodID.Text = order.goodId;
                    this.textBoxGoodName.Text = order.goodName;
                    this.textBoxCustomerId.Text = order.customerId;
                    this.textBoxCustomerName.Text = order.customerName;
                    this.textBoxPrice.Text = order.goodPrice;
                    this.textBoxGoodNum.Text = order.goodNum;
                    this.textBoxtotal_prices.Text = order.totalPrices;
                    // this.textBox_money_receive.Text = order.moneyReceive;
                    this.textBoxDiscount.Text = order.discount;
                    this.textBoxReceiver_name.Text = order.receiverName;
                    this.dateTimePickerAdddate.Value = Convert.ToDateTime(order.addDate);
                    //this.textBoxReceiveCompany.Text = order.receiveCompany;
                    this.richTextBoxRemark.Text = order.remark;
                    break;
            }
        }

        private void textBoxPrice_TextChanged_1(object sender, EventArgs e)
        {
            calculateTotalPrice();
        }

        private void textBoxGoodNum_TextChanged(object sender, EventArgs e)
        {
            calculateTotalPrice();
        }

        private void textBoxDiscount_TextChanged(object sender, EventArgs e)
        {
            calculateTotalPrice();
        }

        private void getdatabutton_Click(object sender, EventArgs e)
        {
            string orderid = textBoxOrderId.Text.Trim();
            if (orderid == "")
            {
                MessageBox.Show("请输入订单号");
                return;
            }
            Order order = OrderMethod.getEntityById(orderid);
            if (order != null)
            {
                textBoxGoodID.Text = order.goodId;
                textBoxGoodName.Text = order.goodName;
                textBoxGoodNum.Text = order.goodNum;
                textBoxPrice.Text = order.goodPrice;
                textBoxCustomerId.Text = order.customerId;
                textBoxCustomerName.Text = order.customerName;
                textBoxDiscount.Text = order.discount;
                textBoxtotal_prices.Text = order.totalPrices;
                richTextBoxRemark.Text = order.remark;
                textBoxReceiver_name.Text = order.receiverName;
            }
            else
            {
                MessageBox.Show("没有对应的订单！请检查订单号！");
            }
        }

       
    }
}
